package com.longcai.service.fuyoupay.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.pool.vendor.SybaseExceptionSorter;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.ApiResult;
import com.longcai.entity.order.ClyjGoodsOrderDetails;
import com.longcai.entity.payment.fuyou.request.ClyjFuyouLinePayOrder;
import com.longcai.entity.payment.fuyou.request.ClyjFuyouSubamountRule;
import com.longcai.fenzhuang.fuyou.BaseResponse;
import com.longcai.fenzhuang.fuyou.FuyouCommonPortMethod;
import com.longcai.mapper.fuyoupay.ClyjFuyouLinePayOrderMapper;
import com.longcai.service.fuyoupay.ClyjFuyouLinePayOrderService;
import com.longcai.vo.ClyjGoodsOrderDetailsVo;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author LWK
 * @since 2019-04-11
 */
@Service
public class ClyjFuyouLinePayOrderServiceImpl extends ServiceImpl<ClyjFuyouLinePayOrderMapper, ClyjFuyouLinePayOrder>
		implements ClyjFuyouLinePayOrderService {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private ClyjFuyouLinePayOrderMapper clyjFuyouLinePayOrderMapper;
	@Override
	public ApiResult<?> weixinPay( ClyjGoodsOrderDetailsVo clyjGoodsOrderDetailsVoR) {
		
		ClyjFuyouLinePayOrder cflp = new ClyjFuyouLinePayOrder();
		List<ClyjGoodsOrderDetails> clyjGoodsOrderDetails = clyjGoodsOrderDetailsVoR.getClyjGoodsOrderDetails();
		String splitSsn = clyjGoodsOrderDetailsVoR.getSplitSsn();
		ClyjGoodsOrderDetails details = clyjGoodsOrderDetails.get(0);
		/*
		 * if(StringUtils.isEmpty(params.getString("custName"))||StringUtils.
		 * isEmpty(params.getString("brandName"))||StringUtils.isEmpty(params.
		 * getString("unifiedCode"))||StringUtils.isEmpty(params.getString(
		 * "licenseIndate"))||StringUtils.isEmpty(params.getString("artifNm"))||
		 * StringUtils.isEmpty(params.getString("contact"))||StringUtils.isEmpty
		 * (params.getString("certifId"))||StringUtils.isEmpty(params.getString(
		 * "mobileNo"))||StringUtils.isEmpty(params.getString("BankLicense"))||
		 * StringUtils.isEmpty(params.getString("parentBankId"))||StringUtils.
		 * isEmpty(params.getString("bankNm"))||StringUtils.isEmpty(params.
		 * getString("capacntNo"))||StringUtils.isEmpty(params.getString(
		 * "accessory2"))||StringUtils.isEmpty(params.getString("agreement"))||
		 * StringUtils.isEmpty(params.getString("createTime"))||StringUtils.
		 * isEmpty(params.getString("creater"))){ return
		 * ApiResult.fail(ResultEnum.BAD_REQUEST_PARAMETER); }
		 */
		if(StringUtils.isEmpty(details.getOrderSource())||StringUtils.isEmpty(details.getPayMoney().toString())
				||StringUtils.isEmpty(splitSsn)||StringUtils.isEmpty(details.getGoodsName())){
			return null;
		}
			cflp.setId(UUID.randomUUID().toString().replace("-", ""));
			cflp.setPaymentType("04");
			cflp.setChannel(details.getOrderSource());//交易来源
			cflp.setIntoAccount("");
			cflp.setAmt(details.getPayMoney().toString());
			cflp.setSplitSsn(splitSsn);//分账编号
			cflp.setGoodsType("1");//商品类型
			cflp.setSubject(details.getGoodsName());//商品名称
			cflp.setGoodsDes("111");//商品描述
			cflp.setGoodsQuanity(details.getGoodsAmount().toString());//选填，商品数量
			cflp.setGoodsTag("");//选填 ，商品标记
			cflp.setTermIp("");//终端ip;
			cflp.setNotifyUrl("/clyjFuyouSubamount/subamount");//回调路径
			cflp.setFrontnotifyUrl("www.baidu.com");
			cflp.setOrderType("GZXS");//订单类型
			cflp.setSubOpenid("ooIeqszBlkwLqoyJ2uzCTQzvSCO0");
			cflp.setSubAppid("wxfa089da95020ba1a");
			
			cflp.setCreateTime(System.currentTimeMillis());
			cflp.setSubacountTime(cflp.getCreateTime()+10*24*60*60*1000);
		FuyouCommonPortMethod method = new FuyouCommonPortMethod();
		try {
			BaseResponse r = method.LinePay(cflp);
			if (r.getRespCode().equals("0000")) {
				cflp.setMchntTxnSsn(r.getMchntTxnSsn());
				clyjFuyouLinePayOrderMapper.insert(cflp);
				return ApiResult.success(r);
			}
		} catch (Exception e) {
			 logger.error("查询失败");
	         e.printStackTrace();
		}
		return ApiResult.fail("富友微信支付发起失败");
	}

}
